package edu.uoc.tdp.pac4.client.maintenance;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import edu.uoc.tdp.pac4.beans.Course;
import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.MantenimentInterface;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;


public class CourseSearchWindow extends JPanel {

	private static final long serialVersionUID = 1L;

	
	private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	private ClientConnectionHelper connectionHelper_;
	private MantenimentInterface maintenance;
	
	/**
	 * 
	 */
	private JTextField tfId;
	private JTextField tfName;
	private JTextField tfReqAttend;
	private JTable tbCourseList;
	//private JTextArea tABibl, tAPlaDoc;	
	private JTextField tfCredits;
	private JTextField tfPlaEst;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	//Date avui = new Date(System.currentTimeMillis());
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		LanguageUtils.setDefaultLanguage("i18n/messages");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CourseSearchWindow frame = new CourseSearchWindow(new ClientConnectionHelper());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CourseSearchWindow(final ClientConnectionHelper connectionHelper_) {
		
		setBorder(new TitledBorder(null,
				LanguageUtils
						.getMessage("main.menu.maintenance.course"),
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setLayout(null);

		this.connectionHelper_ = connectionHelper_;
		this.maintenance = connectionHelper_.getMaintenanceManager();
	

		JPanel panelSup = new JPanel();
		panelSup.setBounds(0, 12, 619, 288);
		//panelSup.setBounds(0, 0, 685, 243);
		panelSup.setLayout(null);
		add(panelSup);//,BorderLayout.NORTH
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		//scrollPane.setBounds(0, 242, 685, 243);
		scrollPane.setBounds(6, 300, 619, 120);
		add(scrollPane);

		JPanel panel_1 = new JPanel();
		panel_1.setBounds(6, 450, 619, 37);
		add(panel_1);//,BorderLayout.SOUTH
		
		
		GridBagLayout gbl_panelSup = new GridBagLayout();
		gbl_panelSup.rowHeights = new int[] {30, 30, 30, 30, 30, 30, 0};
		gbl_panelSup.columnWidths = new int[] {30, 90, 150, 100, 90, 90, 90};
		gbl_panelSup.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, 0.0, 0.0};
		gbl_panelSup.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
		panelSup.setLayout(gbl_panelSup);
		
		JLabel labelId = new JLabel(" Id");
		labelId.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelId = new GridBagConstraints();
		gbc_labelId.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelId.anchor = GridBagConstraints.WEST;
		gbc_labelId.insets = new Insets(0, 0, 5, 5);
		gbc_labelId.gridx = 1;
		gbc_labelId.gridy = 0;
		panelSup.add(labelId, gbc_labelId);
		
		tfId = new JTextField();
		GridBagConstraints gbc_tfId = new GridBagConstraints();
		gbc_tfId.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfId.insets = new Insets(0, 0, 5, 5);
		gbc_tfId.gridx = 2;
		gbc_tfId.gridy = 0;
		panelSup.add(tfId, gbc_tfId);
		tfId.setColumns(1);
		
		/**
		JLabel labelActivity = new JLabel("Course Activity");
		labelActivity.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelActivity = new GridBagConstraints();
		gbc_labelActivity.gridwidth = 2;
		gbc_labelActivity.anchor = GridBagConstraints.EAST;
		gbc_labelActivity.insets = new Insets(0, 0, 5, 5);
		gbc_labelActivity.gridx = 3;
		gbc_labelActivity.gridy = 0;
		panelSup.add(labelActivity, gbc_labelActivity);
		
		*/
		
		JLabel labelStatus = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelStatus"));
		labelStatus.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelStatus = new GridBagConstraints();
		gbc_labelStatus.insets = new Insets(0, 0, 5, 5);
		gbc_labelStatus.gridx = 3;
		gbc_labelStatus.gridy = 0;
		panelSup.add(labelStatus, gbc_labelStatus);
		
		JLabel labelName = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelName"));
		labelName.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelName = new GridBagConstraints();
		gbc_labelName.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelName.anchor = GridBagConstraints.WEST;
		gbc_labelName.insets = new Insets(0, 0, 5, 5);
		gbc_labelName.gridx = 1;
		gbc_labelName.gridy = 1;
		panelSup.add(labelName, gbc_labelName);
		
		tfName = new JTextField();
		GridBagConstraints gbc_tfName = new GridBagConstraints();
		gbc_tfName.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfName.insets = new Insets(0, 0, 5, 5);
		gbc_tfName.gridx = 2;
		gbc_tfName.gridy = 1;
		panelSup.add(tfName, gbc_tfName);
		tfName.setColumns(10);
		
		JRadioButton rdbtnAll = new JRadioButton("All");
		rdbtnAll.setBackground(UIManager.getColor("CheckBox.background"));
		buttonGroup.add(rdbtnAll);
		GridBagConstraints gbc_rdbtnAll = new GridBagConstraints();
		gbc_rdbtnAll.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnAll.gridx = 3;
		gbc_rdbtnAll.gridy = 1;
		panelSup.add(rdbtnAll, gbc_rdbtnAll);
		
		JRadioButton rdbtnActive = new JRadioButton("Active");
		rdbtnActive.setBackground(UIManager.getColor("CheckBox.background"));
		buttonGroup.add(rdbtnActive);
		GridBagConstraints gbc_rdbtnActive = new GridBagConstraints();
		gbc_rdbtnActive.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnActive.gridx = 4;
		gbc_rdbtnActive.gridy = 1;
		panelSup.add(rdbtnActive, gbc_rdbtnActive);
		
		JRadioButton rdbtnInactive = new JRadioButton("Inactive");
		rdbtnInactive.setBackground(UIManager.getColor("CheckBox.background"));
		buttonGroup.add(rdbtnInactive);
		GridBagConstraints gbc_rdbtnInactive = new GridBagConstraints();
		gbc_rdbtnInactive.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnInactive.gridx = 5;
		gbc_rdbtnInactive.gridy = 1;
		panelSup.add(rdbtnInactive, gbc_rdbtnInactive);
		
		
		JLabel labelAttend = new JLabel(LanguageUtils.getMessage("client.maintenance.coursemanag.labelAttend"));
		labelAttend.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelAttend = new GridBagConstraints();
		gbc_labelAttend.anchor = GridBagConstraints.WEST;
		gbc_labelAttend.insets = new Insets(0, 0, 5, 5);
		gbc_labelAttend.gridx = 1;
		gbc_labelAttend.gridy = 2;
		panelSup.add(labelAttend, gbc_labelAttend);
		
		tfReqAttend = new JTextField();
		GridBagConstraints gbc_tfReqAttend = new GridBagConstraints();
		gbc_tfReqAttend.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfReqAttend.insets = new Insets(0, 0, 5, 5);
		gbc_tfReqAttend.gridx = 2;
		gbc_tfReqAttend.gridy = 2;
		panelSup.add(tfReqAttend, gbc_tfReqAttend);
		tfReqAttend.setColumns(10);
		
		JLabel labelCred = new JLabel(LanguageUtils.getMessage("client.maintenance.coursemanag.labelCred"));
		labelCred.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelCred = new GridBagConstraints();
		gbc_labelCred.anchor = GridBagConstraints.WEST;
		gbc_labelCred.insets = new Insets(0, 0, 5, 5);
		gbc_labelCred.gridx = 1;
		gbc_labelCred.gridy = 3;
		panelSup.add(labelCred, gbc_labelCred);
		
		
		
		
		
		tfCredits = new JTextField();
		tfCredits.setColumns(10);
		GridBagConstraints gbc_tfCredits = new GridBagConstraints();
		gbc_tfCredits.insets = new Insets(0, 0, 5, 5);
		gbc_tfCredits.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfCredits.gridx = 2;
		gbc_tfCredits.gridy = 3;
		panelSup.add(tfCredits, gbc_tfCredits);
		
				
		
		JLabel labelDegreePlan = new JLabel(LanguageUtils.getMessage("client.maintenance.coursemanag.pladoc"));
		labelDegreePlan.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelDegreePlan = new GridBagConstraints();
		gbc_labelDegreePlan.anchor = GridBagConstraints.WEST;
		gbc_labelDegreePlan.insets = new Insets(0, 0, 5, 5);
		gbc_labelDegreePlan.gridx = 1;
		gbc_labelDegreePlan.gridy = 4;
		panelSup.add(labelDegreePlan, gbc_labelDegreePlan);
		
		
		tfPlaEst = new JTextField();
		tfPlaEst.setColumns(10);
		GridBagConstraints gbc_tfPlaEst = new GridBagConstraints();
		gbc_tfPlaEst.insets = new Insets(0, 0, 5, 5);
		gbc_tfPlaEst.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfPlaEst.gridx = 2;
		gbc_tfPlaEst.gridy = 4;
		panelSup.add(tfPlaEst, gbc_tfPlaEst);
		
		JButton btnSearch = new JButton(LanguageUtils.getMessage("client.maintenance.usermanag.btnSearch"));
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					listCourse();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		GridBagConstraints gbc_btnSearch = new GridBagConstraints();
		gbc_btnSearch.fill = GridBagConstraints.BOTH;
		gbc_btnSearch.insets = new Insets(0, 0, 0, 5);
		gbc_btnSearch.gridx = 1;
		gbc_btnSearch.gridy = 6;
		panelSup.add(btnSearch, gbc_btnSearch);
		
		
		
					
		JButton btnClear = new JButton(LanguageUtils.getMessage("client.maintenance.usermanag.btnClear"));
		GridBagConstraints gbc_btnClear = new GridBagConstraints();
		gbc_btnClear.fill = GridBagConstraints.BOTH;
		gbc_btnClear.insets = new Insets(0, 0, 0, 5);
		gbc_btnClear.gridx = 2;
		gbc_btnClear.gridy = 6;
		panelSup.add(btnClear, gbc_btnClear);
		


		
		JButton btnExit = new JButton(LanguageUtils.getMessage("client.maintenance.usermanag.btnExit"));
		btnExit.setForeground(Color.RED);
		GridBagConstraints gbc_btnExit = new GridBagConstraints();
		gbc_btnExit.insets = new Insets(0, 0, 0, 5);
		gbc_btnExit.fill = GridBagConstraints.BOTH;
		gbc_btnExit.anchor = GridBagConstraints.WEST;
		gbc_btnExit.gridx = 5;
		gbc_btnExit.gridy = 6;
		panelSup.add(btnExit, gbc_btnExit);
		

		
		
		tbCourseList = new JTable();		
		//tbCourseList.setEnabled(false);
		tbCourseList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tbCourseList.setModel(new DefaultTableModel(new Object[][] {}, columnNames));		
		scrollPane.add(tbCourseList);
		scrollPane.setViewportView(tbCourseList);
		
		
		//create
		JButton btnCreate = new JButton("create");
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CourseEditWindow createWindow = new CourseEditWindow(connectionHelper_);
				createWindow.setVisible(true);
			}
		});
		panel_1.add(btnCreate);

		
		//edit
		final JButton btnEdit = new JButton(LanguageUtils.getMessage("client.maintenance.usermanag.btnEdit"));
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int i = tbCourseList.getSelectedRow();
				TableModel model2 = tbCourseList.getModel();
				Integer identificador = Integer.valueOf( model2.getValueAt(i, 0).toString());
				try {
					Course course = maintenance.findCourseById(identificador);
					CourseEditWindow createWindow = new CourseEditWindow(course, connectionHelper_);
					createWindow.setVisible(true);				
					
				} catch (RemoteException e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, e1.getMessage(), null,
							JOptionPane.INFORMATION_MESSAGE);			
				} catch (RuntimeException e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, e1.getMessage(), null,
							JOptionPane.INFORMATION_MESSAGE);			
				}
				
			}
		});
		btnEdit.setEnabled(false);
		panel_1.add(btnEdit);
		
		
		tbCourseList.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int selectedRow = tbCourseList.getSelectedRow();
	            System.out.println("Selected row:" + selectedRow);
	            if (selectedRow == -1) {
	                System.out
	                        .println("Select a row in the table before clicking \"Delete\"");
	                return;
	            } 
	            btnEdit.setEnabled(true);
			}
		});
		
	}		

	//"Id", "Name", "Required Attendance", "Credits", "Degree Plan", "Inactivity Date"
	Object[] columnNames = new String[] {
			LanguageUtils.getMessage("course.id"),
			LanguageUtils.getMessage("course.name"),
			LanguageUtils.getMessage("course.attendance"),
			LanguageUtils.getMessage("course.credits"),
			LanguageUtils.getMessage("course.plan"),
			LanguageUtils.getMessage("course.inactivity")
			};
	
	protected void listCourse() throws RemoteException{

		Course courseX=new Course();
		courseX.setCourseCode("".equals(tfId.getText())?0:Integer.valueOf(tfId.getText()));
		courseX.setCourseName("".equals(tfName.getText())?null:tfName.getText());
		courseX.setCredits("".equals(tfCredits.getText())?0:Integer.valueOf(tfCredits.getText()));
		courseX.setPlaDocent("".equals(tfPlaEst.getText())?null:tfPlaEst.getText());
		courseX.setReqAttendance("".equals(tfReqAttend.getText())?0:Integer.valueOf(tfReqAttend.getText()));
		
		
		List<Course> courses = this.maintenance.findCourses(courseX);
		
		Object[][] data = new Object[courses.size()][columnNames.length];

		for (int i = 0; i < data.length; i++) {
			Course course = courses.get(i);
			data[i] = new Object[] { course.getCourseCode(), course.getCourseName(),
					course.getReqAttendance(), course.getCredits(),
					course.getPlaDocent(), course.getInactivityDate()
					};
		}

		TableModel model = new DefaultTableModel(data, columnNames);
		tbCourseList.setModel(model);
		
		
	}
		
		
	
}


